package com.lyubia.shop.order.controller;

import com.lyubia.shop.api.pojo.Admin;
import com.lyubia.shop.api.service.CartService;
import com.lyubia.shop.api.vo.CartResult;
import com.lyubia.shop.common.result.BaseResult;
import com.lyubia.shop.order.service.OrderService;
import com.lyubia.shop.order.service.PayService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Reference(version = "1.0")
    private CartService cartService;

    @Resource
    private OrderService orderService;

    @Resource
    private PayService payService;

    @GetMapping("/preOrder")
    public String preOrder(HttpSession session, Model model) {
        Admin admin = (Admin) session.getAttribute("user");
        CartResult cartResult = cartService.getCartListInfo(Integer.parseInt(admin.getAdminId().toString()));
        model.addAttribute("cartResult", cartResult);
        return "order/preOrder";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(HttpSession session, Model model) {
        Admin admin = (Admin) session.getAttribute("user");
        BaseResult baseResult = orderService.submitOrder(Integer.parseInt(admin.getAdminId().toString()));
        if (baseResult.getCode() == 200) {
            Map<String,Object> map= (Map<String, Object>) baseResult.getObj();
            model.addAttribute("totalAmount",map.get("totalAmount"));
            model.addAttribute("orderSn",map.get("orderSn"));
            return "order/submitOrder";
        }
        return "error";
    }

    /**
     * 去付款(发起支付请求)
     * @return 支付页面
     */
    @RequestMapping("payment")
    public String payment(String orderSn ,Model model){
        String result= payService.startPayRequest(orderSn);
        if(StringUtils.isBlank(result)){
            return "error";
        }
        model.addAttribute("result",result);
        return "order/payment";
    }

    /**
     * 异步回调
     * @return
     */
    @PostMapping("/notifyCallback")
    public String notifyCallback(@RequestParam(name = "out_trade_no") String outTradeNo,
                                 @RequestParam(name = "total_amount") BigDecimal totalAmount,
                                 @RequestParam(name = "trade_no") String tradeNo,
                                 @RequestParam(name = "trade_status") String tradeStatus,
                                 HttpServletRequest request) {
        BaseResult baseResult = payService.notifyCallback(outTradeNo, totalAmount, tradeNo, tradeStatus, request);
        request.setAttribute("result","success");
        if(baseResult.getCode() !=200){
            request.setAttribute("result","failed");
        }
        return "order/notify_result";
    }

    /**
     * 同步回调
     * @return
     */
    @GetMapping("/returnCallback")
    public String returnCallback(@RequestParam(name = "out_trade_no") String outTradeNo,
                                 @RequestParam(name = "total_amount") BigDecimal totalAmount,
                                 @RequestParam(name = "trade_no") String tradeNo,
                                 HttpServletRequest request) {
        System.out.println("支付同步回调地址....");
        BaseResult baseResult = payService.returnCallback(outTradeNo, totalAmount, tradeNo, request);
        request.setAttribute("result",baseResult);
        //重定向  防止用户刷新浏览器地址栏 订单状态重复更新
        return "redirect:order/paySuccess";

    }

    @GetMapping("/paySuccess")
    public String paySuccess() {
        return "order/pay_result";
    }


}
